package com.it.park.controller.admin;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.it.park.client.service.AppVersionService;
import com.it.park.client.service.VersionService;
import com.it.park.common.JsonObj;
import com.it.park.common.Page;
import com.it.park.constant.Constant;
import com.it.park.model.AppVersion;
import com.it.park.model.Version;
import com.it.park.util.file.FileUtil;

@Controller
@RequestMapping("/admin/log/version")
public class AdminVersionController {
	private String rootPath=Constant.ADMIN_PATH;

	private Page page = null;
	@Autowired
	private VersionService versionService;
	@Autowired
	private AppVersionService appVersionService;
	
	@RequestMapping("listRows")
	@ResponseBody
	public Map<String,Object> listRows(HttpServletRequest request){
			Integer pageNow = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			Map<String,Object> map_ = new HashMap<String, Object>();
			Integer total = versionService.selectTotal(map_);
			page = new Page(pageNow,total,rows);
			map_.put("startPos", page.getStartPos());
			map_.put("pageSize", page.getPageSize());
			List<Version> list = versionService.selectPagination(map_);
			Map<String,Object> map  =new HashMap<String, Object>();
			map.put("total", total);
			map.put("rows", list);
			return map;
	}
	
	@RequestMapping("versionList")
	public String newsList(HttpServletRequest request){
			return rootPath+"log/version/versionList";
	}
	
	@RequestMapping("save")
	@ResponseBody
	public JsonObj save(Version version,HttpServletRequest request){
		JsonObj json = new JsonObj();
		version.setCreateDate(new Date());
		boolean success = versionService.insertSelective(version);
		if(success){
			json.setMessage(Constant.SUCCESS_SAVE_MSG);
			json.setSuccess(Constant.S);
		}else{
			json.setSuccess(Constant.F);
			json.setMessage(Constant.FAIL_SAVE_MSG);
		}
		return json;
	}
	
	@RequestMapping("deleteById")
	@ResponseBody
	public JsonObj deleteById(@RequestParam("id") Integer id,HttpServletRequest request){
		JsonObj json = new JsonObj();
		boolean success = versionService.deleteByPrimaryKey(id);
		if(success){
			json.setMessage(Constant.SUCCESS_DELETE_MSG);
			json.setSuccess(Constant.S);
		}else{
			json.setMessage(Constant.FAIL_DELETE_MSG);
			json.setSuccess(Constant.F);
		}
		return json;
	}
	
	@RequestMapping("edit")
	@ResponseBody
	public JsonObj edit(Version version,HttpServletRequest request){
		JsonObj json = new JsonObj();
		version.setCreateDate(new Date());
		boolean success = versionService.updateByPrimaryKeySelective(version);
		if(success){
			json.setMessage(Constant.SUCCESS_UPDATE_MSG);
			json.setSuccess(Constant.S);
		}else{
			json.setMessage(Constant.FAIL_UPDATE_MSG);
			json.setSuccess(Constant.F);
		}
		return json;
	}
	
	@RequestMapping("uploadApp")
	public String uploadApp(MultipartFile file,AppVersion appVersion,HttpServletRequest request){
		if(!file.isEmpty()){
			try {
				InputStream is =  file.getInputStream();
				appVersion.setCreateDate(new Date());
				appVersion.setVersionContent(FileUtil.getBytes(is));
				if(appVersion.getId() != null){
					appVersionService.updateByPrimaryKeySelective(appVersion);
				}else{
					appVersionService.insertSelective(appVersion);
				}
			} catch (IllegalStateException e) {
				e.printStackTrace();
			}catch (IOException e) {
				e.printStackTrace();
			}
		}
		return "redirect:appVersionList";
	}
	
	@RequestMapping("appDeleteById")
	@ResponseBody
	public JsonObj appDeleteById(@RequestParam("ids") Integer[] ids,HttpServletRequest request){
		JsonObj json = new JsonObj();
		try {
			for (int i = 0; i < ids.length; i++) {
				versionService.deleteByPrimaryKey(ids[i]);
			}
			json.setMessage(Constant.SUCCESS_DELETE_MSG);
			json.setSuccess(Constant.S);
		} catch (Exception e) {
			json.setMessage(Constant.FAIL_DELETE_MSG);
			json.setSuccess(Constant.F);
		}
		return json;
	}
	
	@RequestMapping("appListRows")
	@ResponseBody
	public Map<String,Object> appListRows(HttpServletRequest request){
			Integer pageNow = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			Map<String,Object> map_ = new HashMap<String, Object>();
			Integer total = appVersionService.selectTotal(map_);
			page = new Page(pageNow,total,rows);
			map_.put("startPos", page.getStartPos());
			map_.put("pageSize", page.getPageSize());
			List<AppVersion> list = appVersionService.selectPagination(map_);
			Map<String,Object> map  =new HashMap<String, Object>();
			map.put("total", total);
			map.put("rows", list);
			return map;
	}
	
	@RequestMapping("appVersionList")
	public String appVersionList(HttpServletRequest request){
			return rootPath+"log/version/appVersionList";
	}
}
